<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsBrand extends ActiveRecord{

	const USE_FLG_YES	= 1;		//启用
	const USE_FLG_NO	= 0;		//禁用

    public static function tableName(){
        return '{{%goods_brand}}';
    }

    //根据id获取品牌列表
    public function getBrandListById($brand_id){
        $where = ['id'=>$brand_id];
        $result = $this::find()->where($where)->select('id,logo,country_icon,country_name')->asArray()->all();
        return $result;
    }

    public function getList(){
        $where = ['use_flg'=>1];
        $result = $this::find()->where($where)->select('id,logo,country_icon,brand_name,country_name')->asArray()->all();
        return $result;
    }
    
    public static function brandsRelation($keyword_name){
        if (!$keyword_name){
            return [];
        }
        $sql = 'SELECT brand.id,brand.country_icon,brand.brand_name,brand.brand_desc,banner_config.picture_url,brand.store_location FROM `j_goods_brand` AS brand LEFT JOIN `j_goods_brand_detail` AS brand_detail ON brand.id=brand_detail.brand_id
                LEFT JOIN `j_hot_sale_config` AS brand_pic ON brand_detail.brand_id=brand_pic.brand_id
                LEFT JOIN `j_banner_config` AS banner_config ON brand_pic.id=banner_config.group_id
                WHERE brand.use_flg=1 AND (brand.keyword_name LIKE "%'.$keyword_name.'%" || brand.country_name LIKE "%'.$keyword_name.'%") AND brand_detail.use_flg=1 AND brand_pic.type=9 AND brand_pic.use_flg=1 GROUP BY brand.id ORDER BY brand.id DESC';
        return \common\helps\tools::getDriverDb()->createCommand($sql)->queryAll();
    }
}